class_name Skill_GenerateItem
extends Resource

enum SelectMode
{
	Unique, # 单一的，只会选择第一个
	Sequential, # 顺序的
	Random, # 随机的
}

@export var repeat_times_range:Vector2i = Vector2i.ONE
@export var reset_pre_times:bool = true
@export var angle:Vector2 = Vector2.ZERO
@export var offest_min:Vector2 = Vector2.ZERO
@export var offest_max:Vector2 = Vector2.ZERO
@export var interval_range:Vector2
@export var is_attach:bool = false
@export var select_mode:SelectMode = SelectMode.Unique
var round:Skill_GenerateRound
var cur_scene_idx:int = 0

func reset():
	round.reset()
func create_tween(round:Skill_GenerateRound,tween:Tween):
	self.round = round
	var random_times = randi_range(repeat_times_range.x,repeat_times_range.y)
	for i in range(random_times):
		tween.tween_callback(func():
			var scene:PackedScene = get_scene()
			if reset_pre_times:
				reset()
			self.round.set_angle(randf_range(angle.x,angle.y))
			self.round.set_pos(Vector2(randf_range(offest_min.x,offest_max.x),randf_range(offest_min.y,offest_max.y)))
			var temp = scene.instantiate()
			var entity:Entity = Utils.find_child_by_type(temp,Entity,-1)
			if entity:
				entity.set_init_args({
					Creater = round.cur_item.get_agent().entity
				})
			if is_attach:
				self.round.cur_item.skill.add_child(temp)
			else:
				self.round.cur_item.skill.get_tree().root.add_child(temp)
			temp.global_position = self.round.pos
			temp.global_rotation_degrees = self.round.angle
			)
		if i < random_times - 1:
			var interval = randf_range(interval_range.x,interval_range.y)
			tween.tween_interval(interval)
	return tween
func get_scene():
	match select_mode:
		SelectMode.Unique:
			return self.round.scenes[0]
		SelectMode.Sequential:
			var scene = self.round.scenes[cur_scene_idx]
			cur_scene_idx = (cur_scene_idx + 1) % self.round.scenes.size()
			return scene
		SelectMode.Random:
			return self.round.scenes.pick_random()
